package com.tl.sun.manager;

import android.content.Context;
import android.content.SharedPreferences;

import com.tl.sun.AppContext;

/**
 * Created by apple on 17/4/21.
 */

public class RobustVersionManager {
    //真实的版本信息
    private String relName;
    private int relCode;
    //虚拟的版本信息
    private String vName;
    private int vCode;

    private static RobustVersionManager instance;

    public RobustVersionManager() {
        getVersion(AppContext.getInstance());
    }

    //获取缓存
    private void getVersion(AppContext  context) {
        SharedPreferences sp = context.getSharedPreferences(MDConstant.VERSION_INFO, Context.MODE_PRIVATE);
        relName = sp.getString(MDConstant.REL_NAME, "");
        vName = sp.getString(MDConstant.V_NAME,"");
        relCode = sp.getInt(MDConstant.REL_CODE,0);
        vCode = sp.getInt(MDConstant.V_CODE,0);
    }

    public void saveVersion(){
        SharedPreferences sp = AppContext.getInstance().getSharedPreferences(MDConstant.VERSION_INFO, Context.MODE_PRIVATE);
        sp.edit().putString(MDConstant.REL_NAME,relName)
                .putString(MDConstant.V_NAME,vName)
                .putInt(MDConstant.REL_CODE,relCode)
                .putInt(MDConstant.V_CODE,vCode)
                .apply();
    }

    public void clearVersion(){
        SharedPreferences sp = AppContext.getInstance().getSharedPreferences(MDConstant.VERSION_INFO, Context.MODE_PRIVATE);
        sp.edit().clear().apply();
    }

    public void clearRobustCode(){
        SharedPreferences sp = AppContext.getInstance().getSharedPreferences(MDConstant.VERSION_INFO, Context.MODE_PRIVATE);
        sp.edit().putInt(MDConstant.V_CODE,0).apply();
    }

    public static synchronized RobustVersionManager getInstance(){
        if (instance == null){
            instance = new RobustVersionManager();
        }
        return instance;
    }

    public String getRelName() {
        return relName;
    }

    public void setRelName(String relName) {
        this.relName = relName;
    }

    public int getRelCode() {
        return relCode;
    }

    public void setRelCode(int relCode) {
        this.relCode = relCode;
    }

    public String getvName() {
        return vName;
    }

    public void setvName(String vName) {
        this.vName = vName;
    }

    public int getvCode() {
        return vCode;
    }

    public void setvCode(int vCode) {
        this.vCode = vCode;
    }
}
